package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/")
public class SendMessagesController {
    /**
     * @param phone
     * @param req
     */
    @GetMapping("/getMessage")
    public Map getMessage(@RequestParam String phone, HttpServletRequest req) {

        /* *//*获取phone*//*
        String phone = req.getParameter("phone");
*/
        String phoneString = "" + phone;
        // Map<String, Object> map = new HashMap<>();
        System.out.println(phone);
        Map codeMap = new HashMap();
        int n = 4;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        // 2464
        String codeString = code.toString();
        codeMap.put("code", codeString);

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GGnaDaEZPjGKTCvoJex", "ugHTzBJOZN7Yya5aS3zCM15UTVVmoz");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneString); // 手机号码
        request.putQueryParameter("SignName", "Hooc在线教育平台");   // 签名
        request.putQueryParameter("TemplateCode", "SMS_200714000");    // 验证码模板
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(codeMap));
        System.out.println("code");
        System.out.println(phone + "phone is =");

//        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G81g9rAGV26P82CYURW", "sdtfqz1qSPiRgJ0Xa9Xhp2CjtFcQcf");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        CommonRequest request = new CommonRequest();
//        request.setSysMethod(MethodType.POST);
//        request.setSysDomain("dysmsapi.aliyuncs.com");
//        request.setSysVersion("2017-05-25");
//        request.setSysAction("SendSms");
//        request.putQueryParameter("RegionId", "cn-hangzhou");
//        request.putQueryParameter("PhoneNumbers", "15626288584");
//        request.putQueryParameter("SignName", "shop商城");
//        request.putQueryParameter("TemplateCode", "SMS_203190168");
//        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(codeMap));
//        System.out.println("code");
//        System.out.println(phone + "phone is =");

        /*将验证码存入session*/
        req.getSession().setAttribute("verification", codeString);
        HttpSession session = req.getSession();
        session.setMaxInactiveInterval(300);
        String sessionId = req.getSession().getId();
        req.getSession().invalidate();

        /**
         *   hjh   --->  服务器
         *  访问      你
         *  开启 一次 会话 session
         *  生成一个 sessionId 唯一的id
         *  关闭浏览器  session X
         *
         *  已登录用户   userInfo
         *  Map key -> value
         *    sessionId  -> userInfo
         *
         *  createOrder orderInfo + userInfo
         *
         */


        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将验证码放在返回值中
        return codeMap;
    }
}